package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.AddCourseTeacherDto;
import com.xuecheng.content.model.dto.CourseTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: xuecheng-plus-project
 * @author: lvyaxin
 * @create: 2024-07-23 20:18
 **/
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @Autowired
    private CourseBaseMapper courseBaseMapper;

    @Override
    public List<CourseTeacherDto> selectTeacherList(Long courseId) {
        // 根据课程id查询对应课程的教师
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(
                new LambdaQueryWrapper<CourseTeacher>().eq(CourseTeacher::getCourseId, courseId));

        // 将PO转换为DTO
        List<CourseTeacherDto> courseTeacherDtos = new ArrayList<>();
        for (CourseTeacher courseTeacher : courseTeachers) {
            CourseTeacherDto courseTeacherDto = new CourseTeacherDto();
            BeanUtils.copyProperties(courseTeacher, courseTeacherDto);
            courseTeacherDtos.add(courseTeacherDto);
        }
        return courseTeacherDtos;
    }

    @Override
    public CourseTeacherDto insertTeacherInfo(AddCourseTeacherDto teacherDto, Long companyId) {
        // 将Dto转为Po
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(teacherDto, courseTeacher);

        // 检验是否本课程机构
        checkCompanyId(companyId, courseTeacher.getCourseId(), "只允许向机构自己的课程中添加修课教师信息");

        courseTeacher.setCreateDate(LocalDateTime.now());

        // 写入数据库
        int insert = courseTeacherMapper.insert(courseTeacher);
        if (insert <= 0) {
            throw new RuntimeException("添加教师信息失败");
        }

        return getCourseTeacherInfo(courseTeacher.getId());
    }

    private CourseTeacherDto getCourseTeacherInfo(Long courseTeacherId) {
        // 根据id查询课程教师信息
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(courseTeacherId);

        // 把Po转换为Dto
        CourseTeacherDto courseTeacherDto = new CourseTeacherDto();
        BeanUtils.copyProperties(courseTeacher, courseTeacherDto);

        return courseTeacherDto;
    }

    private void checkCompanyId(Long companyId, Long courseId, String message) {
        // 查询课程所属机构
        CourseBase courseBase = courseBaseMapper.selectById(courseId);

        // 数据合法性检验
        // 根据具体的业务逻辑去检验
        // 只允许向机构自己的课程中修改老师信息
        if (!companyId.equals(courseBase.getCompanyId())) {
            XueChengPlusException.cast(message);
        }
    }

    @Override
    public void deleteTeacherInfo(Long courseId, Long courseTeacherId, Long companyId) {
        // 校验是否本课程机构
        checkCompanyId(companyId, courseId, "只允许向机构自己的课程中删除教师信息");

        // 删除课程教师信息
        courseTeacherMapper.deleteById(courseTeacherId);
    }
}
